package org.linlinjava.litemall.admin.service;

import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminHerbService {

    @Autowired
    private LitemallHerbService herbService;

    public Object list(String code,String herbName,String measurement_unit,String sort, String order,Integer page, Integer limit,boolean selectAll) {
        List<LitemallHerb> HerbList = herbService.querySelective(code, herbName, measurement_unit,sort,order, page, limit,selectAll);
        return ResponseUtil.okList(HerbList);
    }

    public void add(LitemallHerb herbExample) {
        herbExample.setId((int)System.currentTimeMillis());
        herbExample.setAddTime(LocalDateTime.now());
        herbExample.setUpdateTime(LocalDateTime.now());
        herbService.insertSelective(herbExample);
    }

    public void updateById(LitemallHerb herbExample) {
        herbExample.setUpdateTime(LocalDateTime.now());
        herbService.updateByPrimaryKeySelective(herbExample);
    }

    public void delete(LitemallHerb herbExample) {
        herbExample.setUpdateTime(LocalDateTime.now());
        herbExample.setDeleted(true);
        herbService.updateByPrimaryKeySelective(herbExample);
    }

    public boolean checkHerbNameExist(String id,String name) {
        LitemallHerbExample example = new LitemallHerbExample();
        LitemallHerbExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)){
            criteria.andIdNotEqualTo(Integer.valueOf(id));
        }
        criteria.andHerbNameEqualTo(name);
        criteria.andDeletedEqualTo(false);

        return herbService.countByExample(example) != 0;
    }
    public boolean checkCodeExist(String id,String code) {
        LitemallHerbExample example = new LitemallHerbExample();
        LitemallHerbExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)){
            criteria.andIdNotEqualTo(Integer.valueOf(id));
        }
        criteria.andCodeEqualTo(code);
        criteria.andDeletedEqualTo(false);

        return herbService.countByExample(example) != 0;
    }

    public int batchInsert(List<LitemallHerb> list){
        return herbService.batchInsert(list);
    }



}
